package kk.service.impl;

import com.github.pagehelper.PageHelper;
import kk.dao.MemberDao;
import kk.dao.OrdersDao;
import kk.dao.ProductDao;
import kk.dao.TravellerDao;
import kk.domain.Member;
import kk.domain.Orders;
import kk.domain.Traveller;
import kk.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    private OrdersDao ordersDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private ProductDao productDao;

    @Autowired
    private TravellerDao travellerDao;

    @Override
    public List<Orders> findAll() throws Exception {
        List<Orders> ordersList = ordersDao.findAll();
        return ordersList;
    }

    @Override
    public List<Orders> findAll(int pageNum, int pageSize) throws Exception {
        PageHelper.startPage(pageNum,pageSize);
        List<Orders> ordersList = ordersDao.findAll();
        return ordersList;
    }

    @Override
    public Orders findById(Integer id) throws Exception {
        Orders orders = ordersDao.findById(id);
        return orders;
    }

    @Override
    public Orders findOneOrder(Integer id) throws Exception {
        Orders orders = ordersDao.findOneOrder(id);
        return orders;
    }

    @Override
    public void deleteByProductId(Integer pid) throws Exception {
        ordersDao.deleteByProductId(pid);
    }

    @Override
    public void deleteByMemberId(Integer mid) throws Exception {
        ordersDao.deleteByMemberId(mid);
    }

    @Override
    public void deleteById(Integer oid) throws Exception {
        //先删除中间表中的数据
        ordersDao.deleteFromOrder_TravellerByOrdersId(oid);
        //再删除主表中的数据
        ordersDao.deleteByOrdersId(oid);
    }

    @Override
    public void update(Orders orders) throws Exception {
        ordersDao.update(orders);
    }

    @Override
    public void add(String orderNum, Date orderTime, Integer peopleCount, String orderDesc, Integer payType, Integer orderStatus,
                    Integer productId,
                    String member_name, String member_nickname, String member_phoneNum, String member_email,
                    String traveller_name, String traveller_sex, String traveller_phoneNum, Integer credentialsType, String credentialsNum, Integer travellerType) throws Exception {

        //对会员信息进行封装
        Member member=new Member();
        member.setName(member_name);
        member.setNickname(member_nickname);
        member.setPhoneNum(member_phoneNum);
        member.setEmail(member_email);

        //对游客信息进行封装
        Traveller traveller=new Traveller();
        traveller.setName(traveller_name);
        traveller.setSex(traveller_sex);
        traveller.setPhoneNum(traveller_phoneNum);
        traveller.setCredentialsType(credentialsType);
        traveller.setCredentialsNum(credentialsNum);
        traveller.setTravellerType(travellerType);


        //然后判断该会员信息是否在数据库中已经存在
        Member isExist = null;
        try {
            isExist=memberDao.findByNameAndPhoneNum(member.getName(), member.getPhoneNum());
        }catch (Exception e){
            e.printStackTrace();
        }
        if (isExist!=null){
            //如果已经存在
            //修改payCount并进行判断level
            int beforePayCount = isExist.getPayCount();
            int nowPayCount = beforePayCount+1;
            String level="";
            isExist.setPayCount(nowPayCount);
            if (nowPayCount>=0 && nowPayCount<=2){
                level="C";
            }else if (nowPayCount>=3 && nowPayCount<=5){
                level="B";
            }else if (nowPayCount>5){
                level="A";
            }
            isExist.setLevel(level);
            //更新信息
            memberDao.update(isExist);
            //获取member的id
            String mid = isExist.getId();

            //封装orders对象
            Orders orders=new Orders();
            orders.setOrderNum(orderNum);
            orders.setOrderTime(orderTime);
            orders.setPeopleCount(peopleCount);
            orders.setOrderDesc(orderDesc);
            orders.setPayType(payType);
            orders.setOrderStatus(orderStatus);
            orders.setProductId(productId);
            orders.setMemberId(Integer.parseInt(mid));

            //保存orders到数据库
            ordersDao.add(orders);
            int orderId = Integer.parseInt(orders.getId());
            //保存traveller到数据库
            travellerDao.add(traveller);
            //获取新增旅客的ID
            int travellerId = Integer.parseInt(traveller.getId());
            //在order_traveller中添加订单和旅客的关联信息
            ordersDao.insertIntoOrder_Traveller(orderId,travellerId);
        }else{
            //如果不存在
            member.setPayCount(1);
            member.setLevel("C");
            //将member新增到数据库中
            memberDao.save(member);
            int mid = Integer.parseInt(member.getId());

            //封装orders对象
            Orders orders=new Orders();
            orders.setOrderNum(orderNum);
            orders.setOrderTime(orderTime);
            orders.setPeopleCount(peopleCount);
            orders.setOrderDesc(orderDesc);
            orders.setPayType(payType);
            orders.setOrderStatus(orderStatus);
            orders.setProductId(productId);
            orders.setMemberId(mid);

            //保存orders到数据库
            ordersDao.add(orders);
            int orderId = Integer.parseInt(orders.getId());
            //保存traveller到数据库
            travellerDao.add(traveller);
            int travellerId = Integer.parseInt(traveller.getId());
            //在order_traveller中间表保存信息
            ordersDao.insertIntoOrder_Traveller(orderId,travellerId);
        }
    }

    @Override
    public List<Orders> findByOrderNum(String orderNum, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Orders> ordersList = ordersDao.findByOrderNum(orderNum);
        return ordersList;
    }
}
